package com.etlmaster.executor.utils;

import com.etlmaster.core.Configuration;
import com.etlmaster.executor.utils.LogWriter;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
public class CompressUtil {
	CompressUtil compressUtil = CompressUtil.getInstance();
	private static CompressUtil instance = new CompressUtil();
	private static String deCompressType;
	private static String[] deCompress;

	private CompressUtil() {
		deCompressType = Configuration.getInstance().getProperty("deCompressionType");
		if (null == deCompressType) {
			LogWriter.addLog("ERROR", "无法获取deCompressionType参数");
		} else {
			deCompress = deCompressType.split("\\|");
		}
	}

	public static CompressUtil getInstance() {
		return instance;
	}

	private boolean isUnCompress(final String uri) {
		if (null == uri)
			return false;
		boolean needUnCompress = false;
		for (String _cpress : deCompress) {
			if (uri.toLowerCase().endsWith(_cpress)) {
				needUnCompress = true;
			}
		}
		return needUnCompress;
	}

	private String getFunction(final String uri) {
		if (null == uri)
			return "";
		for (String _cpress : deCompress) {
			if (uri.toLowerCase().endsWith(_cpress)) {
				return _cpress.replace(".", "");
			}
		}
		return "";
	}

	public String unCompressUri(final String uri) {
		if (null == uri)
			return "";
		StringBuilder retUri = new StringBuilder();
		if (isUnCompress(uri)) {
			String f = getFunction(uri);
			if (null != f && !"".equals(f)) {
				retUri = retUri.append(f).append(":").append(uri);
			} else {
				retUri = retUri.append(uri);
			}
		} else {
			retUri = retUri.append(uri);
		}
		return retUri.toString();
	}

}
